package com.zhifa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhifa.Util.ListToJasonArray;
import com.zhifa.Util.ResponseUtil;
import com.zhifa.Util.StringUtil;
import com.zhifa.entity.Admin;
import com.zhifa.entity.Role;
import com.zhifa.service.AuthService;
import com.zhifa.service.RoleService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/auth")
public class AuthController {

	@Resource
	AuthService authService;
	
	@Resource
	RoleService roleService;
	
	@RequestMapping("/createtree")
	public String create(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String,Object> map=new HashMap<String,Object>();
		HttpSession session = request.getSession();
		String parentid = request.getParameter("parentid");
		Admin admin=(Admin) session.getAttribute("currentAdmin");
		Role role=roleService.findById(admin.getRoleid());
		map.put("parentid", parentid);
		List<Integer> ids = StringUtil.stringToList(role.getAuthids());
		map.put("ids", ids);
		JSONArray treeJsonArray = ListToJasonArray.getTreeJsonArray(authService,map);
		ResponseUtil.write(response, treeJsonArray);
		return null;
	}
	
	
}
	